﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;

namespace QExcel
{
    public class QBaseRow
    {
        /// <summary>
        /// 所属表
        /// </summary>
        public QTable Table
        {
            get;
            set;
        }

        /// <summary>
        /// 行名(用于指定样式用)
        /// </summary>
        public virtual string Name
        {
            get;
            set;
        }

        /// <summary>
        /// 当前行处理第几行
        /// </summary>
        public int Index
        {
            get
            {
                int indexValue = 0;
                if (this.Parent != null)
                {
                    foreach (QBaseRow qrow in this.Parent.leafRows)
                    {
                        if (qrow.curIndex < this.curIndex)
                        {
                            indexValue++;
                        }
                    }
                    return this.Parent.Index + indexValue;
                }
                else
                {
                    foreach (QBaseRow row in this.Table.Rows.Rows)
                    {
                        if (row.curIndex < this.curIndex)
                        {
                            indexValue += row.RowCount;
                        }
                    }
                    return indexValue;
                }
            }
        }

        /// <summary>
        /// 行背景色
        /// </summary>
        public Color BgColor
        {
            get;
            set;
        }

        /// <summary>
        /// 行数,包括子行
        /// </summary>
        public int RowCount
        {
            get
            {
                rowCount = 0;
                this.GetRowCount(this);
                return rowCount;
            }
        }

        int rowCount = 0;
        /// <summary>
        /// 递旭获取子行
        /// </summary>
        /// <param name="row"></param>
        private void GetRowCount(QBaseRow row)
        {
            if (row.leafRows.Count == 0)
            {
                rowCount++;
            }
            else
            {
                foreach (QBaseRow child in row.leafRows)
                    this.GetRowCount(child);
            }
        }

        private List<QBaseRow> leafRows = new List<QBaseRow>();
        /// <summary>
        /// 子行
        /// </summary>
        public List<QBaseRow> LeafRows
        {
            get
            {
                return leafRows;
            }
        }

        /// <summary>
        /// 当前处理的第一层节点的第几行
        /// </summary>
        private int curIndex = 0;


        /// <summary>
        /// 父行
        /// </summary>
        public QBaseRow Parent
        {
            get;
            set;
        }

        /// <summary>
        /// 设置当为第几行
        /// </summary>
        /// <param name="index"></param>
        public void SetCurIndex(int index)
        {
            curIndex = index;
        }

        /// <summary>
        /// 添加子行
        /// </summary>
        /// <param name="leafRow"></param>
        public void AddLeafRow(QBaseRow leafRow)
        {
            leafRow.Table = this.Table;
            leafRow.curIndex = this.Table.Rows.Count + 1;
            leafRow.Parent = this;
            this.LeafRows.Add(leafRow);
        }

        /// <summary>
        /// 关联对象
        /// </summary>
        public object Entity
        {
            get;
            set;
        }

        /// <summary>
        /// 获取单元格数据
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public virtual object GetValue(QColumn col)
        {
            Type type = Entity.GetType();
            PropertyInfo info = type.GetProperty(col.Name);
            if (info != null)
            {
                return info.GetValue(Entity, null).ToString();
            }
            return string.Empty;
        }
    }
}
